package org.phpsx.amun.newsreader;

import java.awt.Component;
import java.awt.Image;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

/**
 * CommentsTable
 * 
 * @author     Christoph Kappestein <k42b3.x@gmail.com>
 * @license    http://www.gnu.org/licenses/gpl.html GPLv3
 * @link       http://amun.phpsx.org
 * @category   example
 * @package    newsreader
 * @version    $Revision: 633 $
 */
public class CommentsTable extends JTable
{
	protected TableCellRenderer cellRenderer;

	public CommentsTable(CommentsModel model, TableColumnModel cm)
	{
		super(model, cm);

		setRowHeight(200);

		cellRenderer = new CommentsRenderer();
	}

	public TableCellRenderer getCellRenderer(int row, int column)
	{
		return cellRenderer;
	}

	class CommentsRenderer implements TableCellRenderer
	{
		private Hashtable<URL, Image> thumbnailCache;
		private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			if(value != null)
			{
				try
				{
					CommentsModel tm = (CommentsModel) getModel();

					// get values
					String content = (String) tm.getValueAt(row, 2);
					Date date = sdf.parse((String) tm.getValueAt(row, 3));
					String user = (String) tm.getValueAt(row, 4);
					String thumbnail = (String) tm.getValueAt(row, 6);

					// build editor pane
					String html = "<html><body><table><tr><td width=\"50\" valign=\"top\"><img src=\"" + thumbnail + "\" /></td><td><p><small>created on: " + sdf.format(date) + " by " + user + "</small></p>" + content + "</td></tr></table></body></html>";

					JEditorPane pane = new JEditorPane("text/html", html);
					pane.setSize(table.getSize().width, Integer.MAX_VALUE);
					pane.setEditable(false);

					HTMLEditorKit kit = new HTMLEditorKit();
					pane.setEditorKit(kit);

					StyleSheet styleSheet = kit.getStyleSheet();
					styleSheet.addRule("body {font-family:\"Helvetica Neue\", Arial, Helvetica, sans-serif;}");
					//styleSheet.addRule("h1 {margin:0px;padding:0px;}");
					styleSheet.addRule("p {margin:2px;padding:2px;}");
					styleSheet.addRule("blockquote {font-style:italic;}");

					Document doc = kit.createDefaultDocument();
					pane.setDocument(doc);
					pane.setText(html);
					pane.setBorder(new EmptyBorder(5, 5, 5, 5));

					if(hasFocus)
					{
						/*
						pane.setOpaque(true);
						pane.setBackground(SystemColor.activeCaption);
						pane.setForeground(SystemColor.activeCaptionText);
						*/
					}

					// load images
					Dictionary thumbnailCache = (Dictionary) pane.getDocument().getProperty("imageCache");

					if(thumbnailCache == null)
					{
						thumbnailCache = new Hashtable<URL, Image>();

						pane.getDocument().putProperty("imageCache", thumbnailCache);
					}

					try
					{
						URL thumbnailUrl = new URL(thumbnail);

						if(thumbnailCache.get(thumbnailUrl) == null)
						{
							thumbnailCache.put(thumbnailUrl, (new ImageIcon(thumbnailUrl)).getImage());
						}
					}
					catch(Exception e)
					{
					}

					// set height
					if(pane.getPreferredSize().height != table.getRowHeight(row))
					{
						table.setRowHeight(row, pane.getPreferredSize().height);
					}

					return pane;
				}
				catch(Exception e)
				{
				}
			}

			return null;
		}
	}
}
